package com.etime.controller;

import com.etime.pojo.Station;
import com.etime.service.StationService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("station")
public class StationController {
    @Autowired
    StationService stationService;
    @RequestMapping("getStationByPage")
    public ModelAndView getStationByPage(Integer page, ModelMap modelMap){
        PageInfo<Station> pageInfo=stationService.getStationByPage(page);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("back/station");
        modelMap.addAttribute("data",pageInfo);
        return modelAndView;
    }
    @RequestMapping("getStationByName")
    public ModelAndView getStationByName(Integer page,String station_name,ModelMap modelMap){
        PageInfo<Station> pageInfo=stationService.getStationByName(page,station_name);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("back/station");
        modelMap.addAttribute("data",pageInfo);
        modelMap.addAttribute("station_name",station_name);
        return modelAndView;
    }
    @RequestMapping("addStation")
    public String addStation(Station station){
        boolean result = stationService.addStation(station);
        if (result) {
            //成功
            return "redirect:getStationByPage?page=1";
        } else {
            //失败
            return "redirect:../back/addstation.jsp";
        }
    }
    @RequestMapping("getStationById")
    public ModelAndView getStationById(Integer station_id,ModelMap modelMap){
        Station station =stationService.getStationById(station_id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("back/editstation");
        modelMap.addAttribute("station",station);
        return modelAndView;
    }
    @RequestMapping("editStation")
    public String editStation(Station station){
        boolean result=stationService.ediStation(station);
        if (result) {
            //成功
            return "redirect:getStationByPage?page=1";
        } else {
            //失败
            return "redirect:getStationById? station_id="+station.getStation_id();
        }
    }
    @RequestMapping("getAllStation")
    public ModelAndView getAllStation(ModelMap modelMap){
        List<Station> list=stationService.getAllStation();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("back/addticket");
        modelMap.addAttribute("stationlist",list);
        return modelAndView;

    }

}


